package com.bbz.crawler.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.plugins.Page;
import com.bbz.crawler.core.commen.model.CrawlerProxyIp;
import com.bbz.crawler.core.proxyip.processor.IpProxyPoolProcessor;
import com.bbz.crawler.service.ICrawlerProxyIpService;

/**
 * <p>
 * CrawlerProxyIpController
 * </p>
 *
 * @author binbin.a.zhang123
 * @since 2018-05-29
 */
@RestController
@RequestMapping("/crawlerProxyIp")
public class CrawlerProxyIpController {

	@Autowired
	private ICrawlerProxyIpService crawlerProxyIpService;

	@Autowired
	private IpProxyPoolProcessor ipProxyPoolProcessor;

	/**
	 * 执行爬取代理Ip
	 */
	@RequestMapping("/run")
	public void run() {
		ipProxyPoolProcessor.run();
	}

	/**
	 * 跳转到添加页面CrawlerProxyIp
	 */
	@RequestMapping("/addCrawlerProxyIp")
	public String crawlerProxyIpAdd() {
		return "addCrawlerProxyIp.html";
	}

	/**
	 * 跳转到编辑页面CrawlerProxyIp
	 */
	@RequestMapping("/editCrawlerProxyIp/{crawlerProxyIpId}")
	public String editCrawlerProxyIp(@PathVariable Integer crawlerProxyIpId, ModelMap model) {
		CrawlerProxyIp crawlerProxyIp = crawlerProxyIpService.selectById(crawlerProxyIpId);
		model.addAttribute("item", crawlerProxyIp);
		return "editCrawlerProxyIp.html";
	}

	/**
	 * 获取CrawlerProxyIp列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(String condition) {
		return crawlerProxyIpService.selectList(null);
	}

	/**
	 * 获取CrawlerProxyIp列表
	 */
	@RequestMapping(value = "/list/page/{page}/size/{size}")
	@ResponseBody
	public Object listPage(@PathVariable Integer page, @PathVariable Integer size) {
		return crawlerProxyIpService.selectMapsPage(new Page<>(page, size), null);
	}

	/**
	 * 新增CrawlerProxyIp
	 */
	@RequestMapping(value = "/add")
	public Object add(CrawlerProxyIp crawlerProxyIp) {
		crawlerProxyIpService.insert(crawlerProxyIp);
		return null;
	}

	/**
	 * 删除CrawlerProxyIp
	 */
	@RequestMapping(value = "/delete")
	public Object delete(@RequestParam Integer crawlerProxyIpId) {
		crawlerProxyIpService.deleteById(crawlerProxyIpId);
		return null;
	}

	/**
	 * 修改CrawlerProxyIp
	 */
	@RequestMapping(value = "/update")
	public Object update(CrawlerProxyIp crawlerProxyIp) {
		crawlerProxyIpService.updateById(crawlerProxyIp);
		return null;
	}

	/**
	 * CrawlerProxyIp详情
	 */
	@RequestMapping(value = "/detail/{crawlerProxyIpId}")
	public Object detail(@PathVariable("crawlerProxyIpId") Integer crawlerProxyIpId) {
		return crawlerProxyIpService.selectById(crawlerProxyIpId);
	}
}
